package com.polaris.live.adapter.diamond

import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.util.DateUtils
import com.polaris.live.databinding.AdapterDiamondBinding
import com.polaris.live.resp.back_resp.DiamondResp
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.avatar

/**
 * DiamondAdapter
 *
 * @author Created by 半仙 on 2023/10/26/026 16:57
 */
class DiamondAdapter : BaseBindingAdapter<AdapterDiamondBinding, DiamondResp>() {

    private var mIsGameDetail = false

    override fun convert(holder: VBViewHolder<AdapterDiamondBinding>, item: DiamondResp) {
        holder.vb.apply {
            val position = holder.absoluteAdapterPosition

            if (item.nickname.isNullOrEmpty()) {
                tvDiamondText.text = item.typeName
                tvDiamondText.setTextColor(ColorUtils.getColor(R.color.colorPrimaryText))
            } else {
                val span = SpanUtils.with(tvDiamondText)
                    .append(item.nickname)
                    .setForegroundColor(ColorUtils.getColor(R.color.colorPrimaryText))
                    .append("(${item.typeName})")
                    .setForegroundColor(ColorUtils.getColor(R.color.color_ffaa01))
                    .create()
                tvDiamondText.text = span
            }

            if (item.coin >= 0) {
                if (tvDiamondMoney.context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
                    tvDiamondMoney.text = if (item.coin > 0) "${item.coin} +" else "${item.coin}"
                } else {
                    tvDiamondMoney.text = "+${item.coin}"
                }

                tvDiamondMoney.setTextColor(ColorUtils.getColor(R.color.color_ffaa01))
            } else {
                if (tvDiamondMoney.context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
                    tvDiamondMoney.text = if (item.coin < 0) "${-item.coin} -" else "${item.coin}"
                } else {
                    tvDiamondMoney.text = "${item.coin}"
                }

                tvDiamondMoney.setTextColor(ColorUtils.getColor(R.color.f202028))
            }
            tvDiamondTime.text = TimeUtils.millis2String(item.createTime, "HH:mm").convertArabicDigitsToNormal()
            tvTime.text = TimeUtils.millis2String(item.createTime, "yyyy-MM-dd").convertArabicDigitsToNormal()
            if (!mIsGameDetail) {
                showImage(ivDiamond, item.userCoinOptionType, item.avatar, item.icon)
                ivDiamond.visibility = View.VISIBLE
            } else {
                ivDiamond.visibility = View.GONE
                ivIconImg.setImageResource(R.drawable.ic_game_beans)
            }

            if (position == 0) {
                tvTime.visibility = View.VISIBLE
            } else {
                if (DateUtils.isOneDay(data[position - 1].createTime, item.createTime)) {
                    tvTime.visibility = View.GONE
                } else {
                    tvTime.visibility = View.VISIBLE
                }
            }
        }
    }

    fun setIsGameDetail(isGameDetail: Boolean) {
        mIsGameDetail = isGameDetail
        notifyDataSetChanged()
    }

    private fun showImage(ivDiamond: ImageView, coinType: Int?, avatar: String?, icon: String?) {
        if (avatar.isNullOrEmpty()) {
            if (icon.isNullOrEmpty()) {
                when (coinType) {
                    RECHARGE, TRADER_PURCHASE, RECHARGE_ACTIVITY -> ivDiamond.setImageResource(R.drawable.diamond__recharge)
                    WITHDRAW -> ivDiamond.setImageResource(R.drawable.diamond__withdraw)
                    VIDEO_CALL -> ivDiamond.setImageResource(R.drawable.diamond__call)
                    COMMON_GIFT, LUCKY_GIFT, LUCKY_GIFT_BACK -> ivDiamond.setImageResource(R.drawable.diamond__gift)
                    OFFICIAL_GIFT -> ivDiamond.setImageResource(R.drawable.ic_diamond_detail_type)
                    ABNORMAL_INCOME -> ivDiamond.setImageResource(R.drawable.diamond__abnormal)
                    LIVE_REWARD, GAME_REWARD, GAME_REWARD2 -> ivDiamond.setImageResource(R.drawable.diamond__game)
                    in gameListType -> ivDiamond.setImageResource(R.drawable.diamond_party__game)
                    else -> ivDiamond.setImageResource(R.drawable.diamond__other)
                }
            } else {
                Glide.with(ivDiamond)
                    .load(ImageUtils.imgPath(icon))
                    .circleCrop()
                    .into(ivDiamond)
            }
        } else {
            Glide.with(ivDiamond)
                .load(ImageUtils.imgPathSmall(avatar))
                .circleCrop()
                .avatar()
                .into(ivDiamond)
        }
    }

    companion object {

        const val RECHARGE = 1               //充值
        const val WITHDRAW = 2               //提现
        const val VIDEO_CALL = 3             //通话
        const val COMMON_GIFT = 4            //普通礼物
        const val LUCKY_GIFT = 5             //幸运礼物
        const val LUCKY_GIFT_BACK = 501      //幸运礼物返奖
        const val OFFICIAL_GIFT = 6          //官方赠送
        const val ABNORMAL_INCOME = 7        //官方扣除
        const val LIVE_REWARD = 8            //游戏分成
        const val GAME_REWARD = 801          //游戏奖励
        const val GAME_REWARD2 = 802         //游戏分成
        const val TRADER_PURCHASE = 901      //币商交易
        const val GAME_COIN_BUY = 10         //购买游戏币
        const val LUDO = 1010                //飞行棋
        const val LUDO_CALLBACK = 1011       //飞行棋返奖
        const val LUDO_SHARE = 1012          //飞行棋分成
        const val UNNO = 1020                //UNNO
        const val UNNO_CALLBACK = 1021       //UNNO返奖
        const val UNNO_SHARE = 1022          //UNNO分成
        const val DOMINO = 1030              //多米诺
        const val DOMINO_CALLBACK = 1031     //多米诺返奖
        const val DOMINO_SHARE = 1032        //多米诺分成
        const val SNOOKER = 1040             //斯诺克
        const val SNOOKER_CALLBACK = 1041    //斯诺克返奖
        const val SNOOKER_SHARE = 1042       //斯诺克分成
        const val DAILY_TASKS = 11           //每日任务
        const val RECHARGE_ACTIVITY = 101    //充值特惠活动

        val gameListType = setOf(
            LUDO, LUDO_CALLBACK, LUDO_SHARE,
            UNNO, UNNO_CALLBACK, UNNO_SHARE,
            DOMINO, DOMINO_CALLBACK, DOMINO_SHARE,
            SNOOKER, SNOOKER_CALLBACK, SNOOKER_SHARE,
        )
    }
}
